/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.webapp.controller.management.organization;

import com.alibaba.fastjson.JSONObject;
import com.qinyeit.serviceapp.entity.PrintSettingOrder;
import com.qinyeit.serviceapp.entity.enums.PrintSettingOrderType;
import com.qinyeit.serviceapp.service.organization.PrintSettingOrderService;
import com.qinyeit.webapp.controller.management.BaseManagementController;
import com.qinyeit.webapp.utils.CurrentAccountUtils;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.web.annotation.PermissionInfo;
import com.qinyetech.springstage.core.web.dto.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: PrintSettingOrdersController <br/>
 * Function: printSettingOrder [单据打印设置] <br/>
 * date: 2019-03-04 16:15:43 <br/>
 *
 * @author liuwenjin
 * @version  1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/management/organization/printSettingOrders")
@PermissionInfo(parentName ="基础信息管理" ,parentOrder = 10,order = 10,code = "management_printSettingOrder:read",name ="单据打印设置",deleteAble = false)
public class PrintSettingOrdersController extends BaseManagementController{

	@Autowired
	private PrintSettingOrderService printSettingOrderService;
	private final String [] keys = {"id","printType","name"};


    /**
     * 打印类型
     * @param
     * @return
     */
    @GetMapping("/create")
    public ResponseResult<QyMap> _new(){
        QyMap map=QyMap.NEW();
        map.setv("printData",printSettingOrderService.getPrintSettings( CurrentAccountUtils.getAuthenticatedMerchantGroupID()));
        initFormDate(map);
        return ok("初始化成功", map);
    }

    /**
     * 保存打印设置
     * @param data
     * @param bindingResult
     * @return
     */
    @RequiresPermissions("management_printSettingOrder:create")
    @PermissionInfo(code = "management_printSettingOrder:create",name ="新增",deleteAble = false)
    @PostMapping("/create")
    public ResponseResult<String> create(@RequestBody JSONObject data, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return super.validateFail(bindingResult);
        }
        printSettingOrderService.saveData(data,CurrentAccountUtils.getAuthenticatedMerchantGroupID());
        return ok("保存成功");
    }

    /**
     * 根据类型 读取打印配置
     * @param printType
     * @return
     */
    @GetMapping("/show")
    public ResponseResult<JSONObject> show(PrintSettingOrderType printType){
        QyMap map = printSettingOrderService.getPrintSettingsByType(printType, CurrentAccountUtils.getAuthenticatedMerchantGroupID());
        return ok("查询成功", map);
    }


    @ModelAttribute
    public void preparedData(@PathVariable(value="id",required = false) String id,Model model){
        if(StringUtils.isNotBlank(id)){ //如果路径中有id参数则查询实体对象
            PrintSettingOrder printSettingOrder = printSettingOrderService.findById(Long.valueOf(id));
            model.addAttribute(printSettingOrder);
        }
    }

    /**
     * 初始化表单数据
     * @param qyMap
     */
    private void initFormDate(QyMap qyMap){
        qyMap.setv("printSettingOrderTypes", PrintSettingOrderType.values());
    }

}